﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.InteropServices;
using Excel = Microsoft.Office.Interop.Excel;
using Microsoft.Office.Tools.Excel;

namespace bldgportal
{
    public partial class grid : Form

    {
        WebBrowser gridpage = new WebBrowser();
        HtmlElementCollection rows;
        HtmlElementCollection pagerow;
        HtmlElement row;
        HtmlElement ppgs;
        int lastrow;
        int pagecount;
             

        public grid()
        {
            InitializeComponent();            
            openobservation();            
        }

        private void openobservation()
        {            
            gridpage.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(gridpage_DocumentCompleted);
            gridpage.AllowNavigation = true;
            gridpage.Navigate("http://www.bldgportal.com/DeviationLog/Application/RecordList_Grid.aspx", false);            
        }

        void gridpage_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {            
            HtmlElement table;
            table = gridpage.Document.GetElementById("dataGrid");

            if (table == null)
            {
                newpage();
                rows = table.GetElementsByTagName("tr");
                row = rows[25];                
                pagerow = row.GetElementsByTagName("a");
                pagecount = pagerow.Count;               
                ppgs = pagerow[bldgportal.p];
                ppgs.InvokeMember("click");
            }
            rows = table.GetElementsByTagName("tr");
            row = rows[25];
            pagerow = row.GetElementsByTagName("a");
            pagecount = pagerow.Count;

            if (bldgportal.set == 2 && bldgportal.firstpass == false)
            {
                ppgs = pagerow[pagecount];
                ppgs.InvokeMember("click");
                bldgportal.firstpass = true;
            }

            
            if (bldgportal.p == 10 && bldgportal.set == 1)
            {
                bldgportal.p = 1;
                bldgportal.set = 2;
                newpage();
            }

            ppgs = pagerow[bldgportal.p];
            bldgportal.name = ppgs.InnerHtml;
            if (bldgportal.name != "...")
            {
                bldgportal.pp = Convert.ToInt32(bldgportal.name);
            }

            if (bldgportal.p == 0 && bldgportal.firstpass == false)
            {
                fillrows();                
                ppgs.InvokeMember("click");
                bldgportal.firstpass = true;
                bldgportal.set = 1;             
            }

            else if (bldgportal.p == 0 && bldgportal.firstpass == true)
            {
                bldgportal.firstpass = false;
                fillrows();
                bldgportal.p++;
                gridpage.Dispose();
                newpage();              
                ppgs = pagerow[bldgportal.p];                 
                ppgs.InvokeMember("click");                
            }

            else if (bldgportal.p >= 1 && bldgportal.firstpass == false)
            {
                bldgportal.firstpass = true;                
                ppgs = pagerow[bldgportal.p];
                ppgs.InvokeMember("click");                
            }

            else if (bldgportal.p >= 1 && bldgportal.firstpass == true)
            {
                bldgportal.firstpass = false;
                fillrows();
                bldgportal.p++;
                gridpage.Dispose();
                newpage();
                ppgs = pagerow[bldgportal.p];
                ppgs.InvokeMember("click");
                
            }           
        }

        private void fillrows()
        {
            lastrow = bldgportal.wsc.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell, Type.Missing).Row;
            int rowscount = rows.Count;
            for (int r = 0; r < rowscount - 1; r++)
            {
                HtmlElementCollection observinfo;
                observinfo = rows[r].GetElementsByTagName("td");
                HtmlElement opene;
                opene = observinfo[5];
                string open = opene.InnerHtml;
                //if (open == "0")
                //{
                //    continue;
                //}
                HtmlElement strseq;
                strseq = observinfo[2];
                string ss = strseq.InnerHtml;
                string[] sss = ss.Split(new Char[] { '-' });
                string str = sss[0];
                string seq = sss[1];
                bldgportal.wsc.Cells[lastrow + 1, 1] = str;
                bldgportal.wsc.Cells[lastrow + 1, 2] = seq;
                HtmlElement citye;
                citye = observinfo[3];
                string city = citye.InnerHtml;
                bldgportal.wsc.Cells[lastrow + 1, 3] = city;
                HtmlElement ste;
                ste = observinfo[4];
                string st = ste.InnerHtml;
                bldgportal.wsc.Cells[lastrow + 1, 4] = st;
                bldgportal.wsc.Cells[lastrow + 1, 5] = open;
                lastrow++;
            }

            
        }
        private void newpage()
        {
            gridpage.Dispose();
            iteration it = new iteration();
            it.nextpage();  
        }

        
    }
}
